package cn.com.chb.dakele.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.chb.dakele.util.DateTime;
import cn.com.chb.dakele.util.QianDaoTask;

public class QiandaoServlet extends HttpServlet implements
		ServletContextListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(QiandaoServlet.class);
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		log.info("destroyed_");
	}
	  private static Timer timer = null; 
	  private static QianDaoTask qd = new QianDaoTask();
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		log.info("签到线程已启动");
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.HOUR_OF_DAY, 0);
		cale.set(Calendar.MINUTE, 0);
		cale.set(Calendar.SECOND, 0);
		long mis = cale.getTimeInMillis()+24*60*60*1000+60*1100;
		DateTime date2 = new DateTime(mis/1000);
    	
       timer = new Timer();
      
       timer.schedule(qd, date2, 24*60*60*1000);
       log.info("下次签到时间："+date2.formatTo());
	}

	/**
	 * Constructor of the object.
	 */
	public QiandaoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		log.info("签到servlet以销毁");
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String action = request.getParameter("action"); 
	       if ("shutdown".equals(action)) { 
	          timer.cancel(); 
	          log.info("定时任务已取消"); 
	       } else if ("start".equals(action)) { 
	           timer.cancel(); 
	           
	           log.info("签到线程重新启动");
	           String time = request.getParameter("time");
	           DateTime dd = new DateTime(time);
	          timer = new Timer();
//	         
	          timer.schedule(qd, dd, 24*60*60*1000);
	          log.info("下次签到时间："+dd.formatTo());
	       } 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		log.info("签到servlet以开启");
	}

}
